package com.lq.ecoldchain.ui.mine.carInfo

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.CarInfoEntity
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.toast
import java.util.*

/*
*2018/12/28 0028  14:03
*function by lq
*/
class CarInfoViewModel : BaseViewModel() {
    val carTypeRequestCode= 123
    val maxTon by lazy { MutableLiveData<Int>() }
    val maxCube by lazy { MutableLiveData<Int>() }

    val carType by lazy { MutableLiveData<String>() }//车类型
    val carNum by lazy { MutableLiveData<String>() }//车牌
    val carTon by lazy { MutableLiveData<String>() }//吨位
    val carCube by lazy { MutableLiveData<String>() }//立方
    val lengType by lazy { MutableLiveData<String>() }
    val lengIp by lazy { MutableLiveData<String>() }
    val wenKong by lazy { MutableLiveData<String>() }
    val buyTime by lazy { MutableLiveData<String>() }
    val isGreen by lazy { MutableLiveData<Boolean>() }
    val isRo by lazy { MutableLiveData<Boolean>() }
    val carPlace by lazy { MutableLiveData<Boolean>() }

    val isSuccess by lazy { MutableLiveData<Boolean>() }
    val map by lazy { HashMap<String,Any>() }

    init {
        isGreen.value = true
        isRo.value = true
        carPlace.value = true
        buyTime.value = "${Calendar.getInstance().get(Calendar.YEAR)}"
        obtainCarInfoData()
    }

    fun commit() {
        if (carType.value==null || carNum.value.isNullOrEmpty() || carTon.value == null ||
                carCube.value == null || lengType.value.isNullOrEmpty() || lengIp.value.isNullOrEmpty() || wenKong.value.isNullOrEmpty()) {
            toast { "请填写完善信息" }
        } else {
            val time = buyTime.value!!+"-01-01"
            if (carTon.value!!.toInt() > maxTon.value!!) {
                toast { "吨位应小于或等于该车型的${maxTon.value}吨" }
            } else {
                if (carCube.value!!.toInt() >  maxCube.value!!) {
                    toast { "立方应小于或等于该车型的${carCube.value}立方" }
                } else {
                    map.clear()
                    map["vehicleTypeId"] = carType.value!!
                    map["plateNumber"] = carNum.value!!
                    map["tonnage"] = carTon.value!!
                    map["cube"] = carCube.value!!
                    map["brand"] = lengType.value!!
                    map["modelNumber"] = lengIp.value!!
                    map["temperatureControllerIp"] = wenKong.value!!
                    map["buyingTime"] = time
                    map["isGreenPass"] = isGreen.value!!
                    map["isMeatHook"] = isRo.value!!
                    map["isTailstock"] = carPlace.value!!
                    BaseNetCore.ApiServiceImpl.addTrack(map).compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<String>(){
                        override fun onSuccess(t: String?) {
                            isSuccess.value = true
                            toast { "提交成功" }
                        }

                        override fun onFail(code: String) {
                        }
                    })
                }
            }
        }
    }



    /*获取货车信息*/
    private fun obtainCarInfoData(){
        BaseNetCore.ApiServiceImpl.getTruck().compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<CarInfoEntity>(){
            override fun onSuccess(t: CarInfoEntity?) {
               if(t!=null){
                    carTon.value = "${t.tonnage}"
                   carCube.value = "${t.cube}"
                   carType.value = "${t.vehicleTypeId}"
                   carNum.value = t.plateNumber
                   lengType.value = t.brand
                   lengIp.value = t.modelNumber
                   wenKong.value = t.temperatureControllerIp
//                   buyTime.value = t.buyingTime
                   isGreen.value = t.isGreenPass
                   isRo.value = t.isMeatHook
                   carPlace.value = t.isTailstock
               }
            }
            override fun onFail(code: String) {}
        })
    }
}